﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CQLQueries;
using CQLQueries.Analyzers;
using daSyntaxAnalyser.SyntaxTree;
using daBase;
using daDataLayer;
using System.IO;
using danalyser.Properties;
using danalyser.Controllers;

namespace danalyser.Interface.Forms
{
    public partial class CQLQueries1 : Form
    {
        public CQLQueries1()
        {
            InitializeComponent();
        }

        private void CQLQueries1_Load(object sender, EventArgs e)
        {
            CQL_LA.Instance.ErrorOccuredEvent += new EventHandler<ErrorArgs>(ErrorOccered);
            CQL_SA.Instance.ErrorOccuredEvent += new EventHandler<ErrorArgs>(ErrorOccered);
            CQL_Semantic.Instance.ErrorOccuredEvent += new EventHandler<ErrorArgs>(ErrorOccered);
            CQL_Interpreter.Instance.ErrorOccuredEvent += new EventHandler<ErrorArgs>(ErrorOccered);
            CQL_Interpreter.Instance.MsgEvent += new EventHandler<MsgArg>(GetMsg);
        }

        public void ErrorOccered(object sender, ErrorArgs e)
        {
            RezulData.Rows.Add();
            RezulData.Rows[RezulData.RowCount - 1].Cells[0].Value = Resources.Msg3;
            RezulData.Rows[RezulData.RowCount - 1].Cells[1].Value = e.Level + ": " + e.Message;
            //RezulData.Rows[RezulData.RowCount - 1].Cells[2].Value = e.Position;
            RezulData.Rows[RezulData.RowCount - 1].Cells[4].Value = 4;
            RezulData.Rows[RezulData.RowCount - 1].Cells[5].Value = e.BeginTok.ToString();
            RezulData.Rows[RezulData.RowCount - 1].Cells[6].Value = e.EndTok.ToString();
        }

        public void GetMsg(object sender, MsgArg e)
        {
            RezulData.Rows.Add();
            RezulData.Rows[RezulData.RowCount - 1].Cells[0].Value = Resources.Msg2;
            RezulData.Rows[RezulData.RowCount - 1].Cells[1].Value = e.Msg;
            RezulData.Rows[RezulData.RowCount - 1].Cells[4].Value = 3;
            RezulData.Rows[RezulData.RowCount - 1].Cells[5].Value = e.Beg.ToString();
            RezulData.Rows[RezulData.RowCount - 1].Cells[6].Value = e.End.ToString();
            RezulData.Rows.Add();
            RezulData.Rows[RezulData.RowCount - 1].Cells[0].Value = Resources.Msg2;
            RezulData.Rows[RezulData.RowCount - 1].Cells[1].Value = "Количество возвращённых ISyntaxElement = " + CQL_Interpreter.Instance.Rezult.Count;
            RezulData.Rows[RezulData.RowCount - 1].Cells[4].Value = 2;
            int i = 1;
            foreach (/*ISyntaxTreeElement*/Object elt in CQL_Interpreter.Instance.Rezult)
            {

                RezulData.Rows.Add();
                RezulData.Rows[RezulData.RowCount - 1].Cells[0].Value = Resources.Msg1;
                RezulData.Rows[RezulData.RowCount - 1].Cells[1].Value = elt.ToString();
                //RezulData.Rows[RezulData.RowCount - 1].Cells[2].Value = elt.StartInfo.Line.ToString();
                //RezulData.Rows[RezulData.RowCount - 1].Cells[2].Value = elt.StartInfo.File;
                RezulData.Rows[RezulData.RowCount - 1].Cells[4].Value = 1;
                i++;
            }
            CQL_Interpreter.Instance.Rezult.Clear();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            splitContainer1.Panel2Collapsed = true;
            if (ТекстЗапроса.Text == "")
            {
                MessageBox.Show("Нет ни одного CQL-запроса!");
                return;
            }
            //РезультатЗапроса.Items.Clear();
            RezulData.Rows.Clear();
            String Query = ТекстЗапроса.Text;
            List<String> fn = new List<string>();
            if (SourceFiles.Items.Count == 0)
            {
                MessageBox.Show("Отсутствуют файлы для статического анализа!");
                return;
            }
            for (int i = 0; i < SourceFiles.Items.Count; ++i)
            {
                fn.Add(SourceFiles.Items[i].ToString());
            }
            if (CQL_Interpreter.Instance.ExecuteQuery(Query, fn))
            {
                //РезультатЗапроса.Items.Add("Количество возвращённых ISyntaxElement = " + CQL_Interpreter.Instance.Rezult.Count);
                //int i = 1;
                //foreach (ISyntaxTreeElement elt in CQL_Interpreter.Instance.Rezult)
                //{
                //    РезультатЗапроса.Items.Add("[" + i.ToString() + "] " + elt.ToString());
                //    i++;
                //}
            }
        }
    }
}
